// Tags:
#include <cstdio>
#include <vector>

const int N = 2e5 + 5, M = 22;
int n, m, q, tab[M][M], c[N], f[M][N];
int e_cnt, heads[N];
bool ans[N << 1];

struct Query {
  std::vector<int> a;
  int id;
  Query(int id) : id(id) { a.clear(); }
};

std::vector<Query> Q[N];

struct Edge {
  int v, nxt;
} e[N << 1];

inline void add(int u, int v) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
}

int find(int id, int x) {
  return f[id][x] == x ? x : find(id, f[id][x]);
}

void solve(int x, int fa) {
  for (std::vector<Query>::iterator iter = Q[x].begin(), end = Q[x].end();
       iter != end; ++iter) {
    int tmp = 1;
    ans[iter->id] = false;
    for (std::vector<int>::iterator i = iter->a.begin(), iend = iter->a.end();
         i != iend; ++i) {
      tmp = find(*i, tmp);
      if (tmp == x && tab[*i][c[x]]) {
        ans[iter->id] = true;
        break;
      }
    }
  }
  for (int i = heads[x], v; i; i = e[i].nxt) {
    if ((v = e[i].v) != fa) {
      for (int j = 1; j <= m; ++j) { f[j][x] = tab[j][c[x]] ? v : x; }
      solve(v, x);
    }
  }
  for (int j = 1; j <= m; ++j) { f[j][x] = x; }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("tree.in", "r", stdin);
  freopen("tree.out", "w", stdout);
#endif
#endif

  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= m; ++j) { scanf("%d", &tab[i][j]); }
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      f[i][j] = j;
    }
  }
  for (int i = 1; i <= n; ++i) scanf("%d", &c[i]);
  for (int i = 1, u, v; i < n; ++i) {
    scanf("%d%d", &u, &v);
    add(u, v), add(v, u);
  }
  for (int i = 1, k, p; i <= q; ++i) {
    scanf("%d%d", &k, &p);
    Q[p].push_back(Query(i));
    std::vector<Query>::iterator iter = Q[p].end() - 1;
    for (int i = 1, tmp; i <= k; ++i) {
      scanf("%d", &tmp);
      iter->a.push_back(tmp);
    }
  }
  solve(1, 1);
  for (int i = 1; i <= q; ++i) puts(ans[i] ? "1" : "0");
  return 0;
}